<?php

namespace app\admin\controller;

use app\admin\service\AuthService;
use app\admin\service\CommonService;
use app\admin\service\MenuService;
use app\models\AuthModel;
use app\models\CompanyModel;
use app\models\MenuModel;

/**
 * Class Auth.
 */
class Auth extends Common
{
	/**
	 * 集团管理模型.
	 *
	 * @var CompanyModel
	 */
	protected $companyModel;

	/**
	 * User: wufeng
	 * Date: 2019/4/1
	 *
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function company()
	{
		if ($this->request->isAjax()) {
			$company_id = input('company_id');
			$this->success('success', url('Auth/auth_add', ['company_id' => $company_id]), '', 0);
		}
		$where = [];
		$where['status'] = 1;
		$company = $this->companyModel->where($where)->select();

		return $this->fetch('', [
			'company' => $company
		]);
	}

	/**
	 * 权限管理.
	 * User: wufeng
	 * Date: 2019/4/1
	 *
	 * @return mixed|\think\response\Json
	 */
    public function auth()
    {
        if (request()->isPost()) {
            return json((new AuthService())->auth());
        }
        return $this->fetch('',[
        	'type' => get_current_type(),
			'company_id'=>get_current_company_id()
		]);
    }

    /**
     * 添加权限.
     *
     * @throws \think\exception\DbException
     *
     * @return \think\response\Json|\think\response\View
     */
    public function auth_add()
    {
        if (request()->isPost()) {
            return json((new AuthService())->auth_add());
        }
		return $this->fetch('',[
			'company_id' => input('company_id'),
			'type' => get_current_type()
		]);
    }

    /**
     * 编辑权限.
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return \think\response\Json|\think\response\View
     */
    public function auth_edit()
    {
        if (request()->isPost()) {
            return json((new AuthService())->auth_edit());
        }
        $group = AuthModel::get(input('id'));
        $this->assign('group', $group);
        $this->assign('company_id', input('company_id'));

        return view();
    }

    /**
     * 删除运营组.
     *
     * @return \think\response\Json
     */
    public function auth_del()
    {
        $data = input('post.');
        $group = AuthModel::destroy($data);
        if (false !== $group) {
            return json(['code' => 0, 'msg' => '删除成功！']);
        }

        return json(['code' => 1, 'msg' => '删除失败！']);
    }

    /**
     * 改变运营组状态
     *
     * @return \think\response\Json
     */
    public function auth_check_status()
    {
        $data = input('post.');
        if (0 == $data['status']) {
            $msg = '禁用';
        } else {
            $msg = '激活';
        }
        $group = AuthModel::update($data);
        if (false !== $group) {
            return json(['code' => 0, 'msg' => $msg.'成功！']);
        }
        return json(['code' => 1, 'msg' => $msg.'失败！']);
    }

    /**
     * 分配权限.
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return \think\response\Json|\think\response\View
     */
    public function rule_group()
    {
        if (request()->isPost()) {
            return json((new AuthService())->rule_group());
        }
        $menuModel = new MenuModel();
        $menus = get_column($menuModel->selectAllMenu(), 2);
        $group = AuthModel::get(input('id'));
        $rulesArr = [];
        if ($group['rules']) {
            $rulesArr = explode(',', $group['rules']);
            $this->assign('rulesArr', $rulesArr);
        } else {
            $this->assign('rulesArr', $rulesArr);
        }
        $this->assign('menus', $menus);
        $this->assign('id', input('id'));

        return view();
    }

    /**
     * 菜单管理.
     *
     * @return \think\response\View
     */
    public function menu()
    {
        if (request()->isPost()) {
            return json((new MenuService())->lists());
        }
        $this->assign('pid', input('pid', 0));

        return view();
    }

    /**
     * 添加菜单.
     *
     * @throws \think\exception\DbException
     *
     * @return \think\response\Json|\think\response\View
     */
    public function menu_add()
    {
        if (request()->isPost()) {
            return json((new MenuService())->add());
        }
        $menus = MenuModel::all();
        $list = CommonService::mergeCate($menus, 'pid');
        $this->assign('list', $list);
        $this->assign('id', input('id'));

        return view();
    }

    /**
     * 修改菜单.
     *
     * @throws \think\exception\DbException
     *
     * @return \think\response\Json|\think\response\View
     */
    public function menu_edit()
    {
        if (request()->isPost()) {
            return json((new MenuService())->edit());
        }
        $menuModel = new MenuModel();
        $menu = $menuModel->selectMenuById(input('id'));
        $menus = MenuModel::all();
        $list = CommonService::mergeCate($menus, 'pid');
        $this->assign('list', $list);
        $this->assign('menu', $menu);
        $this->assign('opt', explode('/', $menu['menu_name']));

        return view();
    }

    /**
     * 删除菜单.
     *
     * @return \think\response\Json
     */
    public function menu_del()
    {
        return json((new MenuService())->del(input('id')));
    }

    /**
     * 改变排序.
     */
    public function change_sort()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $menuModel = new MenuModel();
            $url = '';
            if (null === $url && isset($_SERVER['HTTP_REFERER'])) {
                $url = $_SERVER['HTTP_REFERER'];
            } elseif ('' !== $url) {
                $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
            }
            if (false !== $menuModel->where('id', input('id'))->update($data)) {
                return ['code' => 0, 'msg' => '更新成功', 'url' => $url];
            }

            return ['code' => 1, 'msg' => '更新失败'];
        }
    }

	/**
	 *  实例化.
	 */
	protected function _initialize()
	{
		parent::_initialize();
		$this->companyModel = new CompanyModel();
	}
}
